package leetcode.treeCode.week5.leetcode_235;

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return dfs(root,p,q);
    }

    public TreeNode dfs(TreeNode cur, TreeNode p, TreeNode q) {
        if (cur == null) return cur;
        if (cur.val > p.val && cur.val>q.val) {
            TreeNode node = dfs(cur.left, p, q);
            if (node!=null) return node;
        };
        if (cur.val<p.val &&cur.val<q.val) {
            TreeNode node = dfs(cur.right, p, q);
            if (node!=null) return node;
        }
        return cur;
    }
}